<template>
	<view class="container">
		<!-- #ifdef APP-PLUS -->
		<web-view id="webview" :webview-styles="webviewStyles" :src="src" v-show="isShow"></web-view>
		<loading-more v-if="!isShow"></loading-more>
		<!-- #endif -->
		<!-- #ifndef APP-PLUS -->
		<web-view id="webview" :webview-styles="webviewStyles" :src="src"></web-view>
		<!-- #endif -->
	</view>
</template>
<script>
	import loadingMore from "@/components/y-loading/loadingMore.vue";
	export default {
		components: {
			"loading-more": loadingMore
		},
		data() {
			return {
				webviewStyles: {
					// progress: false ,//必须要设置为false
					progress: {
						color: '#FC4141'
					}
				},
				src: '',
				baseUrl: '',
				title: '',

				windowInfo: '',
				isShow: false,
				// 是否是外部链接
				isOther: false,
			};
		},
		onLoad(e) {
			if (e.url != undefined) {
				let webUrl = this.getUrl(e.url);
				if (webUrl.indexOf('http') > -1) {
					this.src = webUrl;
				} else {
					if (webUrl.indexOf('?') < 0) {
						webUrl += "?jwt_token=" + uni.getStorageSync('newToken') + "&tokenObj=" + uni.getStorageSync(
							'newToken_0bj') + "&newCode=" + uni.getStorageSync('newCode')
					} else {
						webUrl += "&jwt_token=" + uni.getStorageSync('newToken') + "&tokenObj=" + uni.getStorageSync(
							'newToken_0bj') + "&newCode=" + uni.getStorageSync('newCode')
					}

					// #ifdef H5
					this.src = "https://" + window.location.host + webUrl;
					// #endif
					// #ifndef H5
					this.src = this.$config.apiUrl + webUrl;
					// #endif
				}
				console.log(this.src, 'webview打开的页面地址：');
				// console.log(this.isOther, 'webview打开的页面是否是外部地址');
			}
		},

		onReady() {
			// #ifdef APP-PLUS
			this.$nextTick(() => {
				this.progressChanged()
			})
			// #endif
		},
		methods: {
			// 多次解码
			getUrl(url) {
				if (url.indexOf('%') < 0) {
					return url
				} else {
					const newUrl = decodeURIComponent(url)
					return this.getUrl(newUrl)
				}
			},
			// 进度条
			progressChanged() {
				let that = this;
				//定义变量用于接收webview对象
				var GOOGLE_MAP_STYLE;
				//定义变量用于接收屏幕宽度
				var SYSTEM_WIDTH;
				//获取webview对象
				var currentWebview = this.$scope.$getAppWebview()
				GOOGLE_MAP_STYLE = currentWebview.children()[0];
				GOOGLE_MAP_STYLE.hide()
				GOOGLE_MAP_STYLE.addEventListener('progressChanged', (e) => {
					if (e.progress == 100) {
						setTimeout(function() {
							that.isShow = true;
							GOOGLE_MAP_STYLE.show();
						}, 1000);
					}
				}, false);
				//如果是页面初始化调用时，需要延时一下
			},
		}
	};
</script>

<style lang="scss">
	.container {
		width: 100%;
		min-height: 100vh;
		background-color: #ffffff;
	}
</style>